﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc.Html;

namespace QuickApp.MVCControl.FlexiGrid
{
    /// <summary>
    /// FlexiGrid的列配置信息
    /// </summary>
    public class FlexiGridColumnSettings
    {
        internal FlexiGridAlign ColumnAlignment { get; private set; }
        internal FlexiGridAlign ColumnHeaderAlignment { get; private set; }
        internal bool ColumnSortable { get; private set; }
        internal string ColumnTitle { get; private set; }
        internal string ColumnWidth { get; private set; }
        internal bool ColumnHidden { get; private set; }
        internal string ColumnTemplate { get; private set; }
        internal string ColumnJavascript { get; private set; }
        internal string ColumnProcess { get; private set; }
        internal bool ColumnToggle { get; private set; }
        internal bool ColumnIsKey { get; private set; }
        internal bool ColumnIsQuickSearch { get; private set; }
        internal bool ColumnIsAdvancedSearch { get; private set; }
        internal string ColumnEnumKey { get; private set; }

        public FlexiGridColumnSettings()
        {
            ColumnWidth = "60";
            ColumnHeaderAlignment = FlexiGridAlign.Center;
            ColumnAlignment = FlexiGridAlign.Center;
            ColumnSortable = true;
        }

        /// <summary>
        /// 设置当前列的宽度 未指定的情况下将置为100
        /// </summary>
        /// <param name="width"></param>
        /// <returns></returns>
        public FlexiGridColumnSettings Width(int width)
        {
            ColumnWidth = width.ToString();
            return this;
        }

        /// <summary>
        /// 设置当前列的宽度 未指定的情况下将置为100
        /// </summary>
        /// <param name="width">宽度，可以是js变量或js函数</param>
        /// <returns></returns>
        public FlexiGridColumnSettings Width(string width)
        {
            ColumnWidth = width;
            return this;
        }

        /// <summary>
        /// 设置当前列的标题
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public FlexiGridColumnSettings Title(string title)
        {
            ColumnTitle = title;
            return this;
        }

        /// <summary>
        /// 设置当前列是否可排序
        /// </summary>
        /// <param name="sortable"></param>
        /// <returns></returns>
        public FlexiGridColumnSettings Sortable(bool sortable = true)
        {
            ColumnSortable = sortable;
            return this;
        }

        /// <summary>
        /// 设置当前列对齐方式
        /// </summary>
        /// <param name="align">对齐方式</param>
        /// <returns></returns>
        public FlexiGridColumnSettings Align(FlexiGridAlign align)
        {
            ColumnAlignment = align;
            return this;
        }

        /// <summary>
        /// 设置当前列对齐方式
        /// </summary>
        /// <param name="align">对齐方式，r：居右，c：居中，l：居左</param>
        /// <returns></returns>
        public FlexiGridColumnSettings Align(string align)
        {
            if (align.Length > 1)
                switch (align[0])
                {
                    case 'r':
                        ColumnAlignment = FlexiGridAlign.Right;
                        break;
                    case 'c':
                        ColumnAlignment = FlexiGridAlign.Center;
                        break;
                    default:
                        //         case 'l':
                        ColumnAlignment = FlexiGridAlign.Left;
                        break;
                }
            return this;
        }

        /// <summary>
        /// 设置列头标题的对齐方式
        /// </summary>
        /// <param name="align">对齐方式</param>
        /// <returns></returns>
        public FlexiGridColumnSettings HeaderAlign(FlexiGridAlign align)
        {
            ColumnHeaderAlignment = align;
            return this;
        }

        /// <summary>
        /// 设置当前列为隐藏列
        /// </summary>
        /// <returns></returns>
        public FlexiGridColumnSettings Hide()
        {
            ColumnHidden = true;
            return this;
        }

        /// <summary>
        /// 希望某列不参与显示隐藏控制，即在生成下拉控制器时判断该属性为真则跳过
        /// </summary>
        /// <returns></returns>
        public FlexiGridColumnSettings Toggle(bool toggle = false)
        {
            ColumnToggle = toggle;
            return this;
        }

        /// <summary>
        /// 是否为快速检索字段
        /// </summary>
        /// <returns></returns>
        public FlexiGridColumnSettings AsQuery(bool enable = true)
        {
            ColumnIsQuickSearch = enable;
            return this;
        }

        /// <summary>
        /// 是否为高级检索字段
        /// </summary>
        /// <returns></returns>
        public FlexiGridColumnSettings AsSearch(bool enable = true)
        {
            ColumnIsAdvancedSearch = enable;
            return this;
        }

        /// <summary>
        /// 为当前列设置显示模板，变量格式为：${name}
        /// </summary>
        /// <returns></returns>
        public FlexiGridColumnSettings Template(string template)
        {
            ColumnTemplate = template;
            return this;
        }

        /// <summary>
        /// 格式化当前列的Javascript脚本，不是函数
        /// 该脚本在执行完Template之后执行，value参数为当前单元格的内容，row为当前行的值
        /// </summary>
        /// <param name="function">显示文本的方法</param>
        /// <returns></returns>
        public FlexiGridColumnSettings Javascript(string content)
        {
            ColumnJavascript = content;
            return this;
        }

        /// <summary>
        /// Process函数, 参数为value，id，row
        /// </summary>
        /// <param name="jsFunction">js函数方法</param>
        /// <returns></returns>
        public FlexiGridColumnSettings Process(string jsFunction)
        {
            ColumnProcess = jsFunction;
            return this;
        }

        /// <summary>
        /// 是否为主键
        /// </summary>
        /// <param name="sortable"></param>
        /// <returns></returns>
        public FlexiGridColumnSettings IsKey()
        {
            ColumnIsKey = true;
            ColumnHidden = true;
            return this;
        }

        /// <summary>
        /// 是否为主键
        /// </summary>
        /// <param name="sortable"></param>
        /// <returns></returns>
        public FlexiGridColumnSettings EnumKey(string enumKey)
        {
            ColumnEnumKey = enumKey;
            ColumnHidden = true;
            return this;
        }

        /// <summary>
        /// 日期格式化
        /// </summary>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public FlexiGridColumnSettings DateFormat(string format)
        {
            ColumnJavascript = string.Format("new Date(Date.parse(value.replace(/-/g,   '/'))).Format('{0}')", format);
            return this;
        }
    }
}
